﻿#region License
// (c) Garry McGlennon 2009
// 
// This project is made available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warrenty – Use at your own risk
// 
// View the project and the latest code at http://dnpframework.codeplex.com/
// 
// All other rights reserved.
// 
// blog: www.dotNetProfessional.com/blog/
#endregion
using dnp.Framework.Database.DatabaseExplorer;

namespace dnp.DomainModeler
{
    public class IntermediateRelationship : DomainIdentifierBase<Relationship>
    {
        public IntermediateRelationship(Table source, string name, bool visibile, Relationship parent) : base(source, name, visibile, parent)
        {
        }

        public new Table Source
        {
            get { return base.Source as Table; }
        }

        public override Domain Domain
        {
            get { return this.Parent.Domain; }
        }

        public Framework.Database.DatabaseExplorer.Relationship LeftRelationship
        {
            get
            {
                Entity parentEntity = this.Parent.Parent;

                // Check if the primary key table is the same as the parent table of this relationship
                // if so then the left side is that relationship.
                if (this.Source.Relationships[0].PrimaryKey.TableFullName == parentEntity.Source.FullName)
                    return this.Source.Relationships[0];
                else
                    return this.Source.Relationships[1];
            }
        }

        public Framework.Database.DatabaseExplorer.Relationship RightRelationship
        {
            get
            {
                Entity parentEntity = this.Parent.Parent;

                // Check if the primary key table is the same as the parent table of this relationship
                // if so then the right side is the other relationship.
                if (this.Source.Relationships[0].PrimaryKey.TableFullName == parentEntity.Source.FullName)
                    return this.Source.Relationships[1];
                else
                    return this.Source.Relationships[0];
            }
        }
    }
}
